load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@rules_cc//cc:cc_library.bzl", "cc_library")

string_flag(
    name = "win-compile",
    build_setting_default = "win-msvc",
    values = [
        "win-msvc",
        "win-mingw",
    ],
    visibility = ["//visibility:public"],
)

config_setting(
    name = "win-msvc",
    flag_values = {":win-compile": "win-msvc"},
)

config_setting(
    name = "win-mingw",
    flag_values = {":win-compile": "win-mingw"},
)

filegroup(
    name = "elf_asm",
    srcs = select({
        "@platforms//cpu:aarch64": [
            "src/asm/jump_arm64_aapcs_elf_gas.S",
            "src/asm/make_arm64_aapcs_elf_gas.S",
            "src/asm/ontop_arm64_aapcs_elf_gas.S",
        ],
        "@platforms//cpu:arm": [
            "src/asm/jump_arm_aapcs_elf_gas.S",
            "src/asm/make_arm_aapcs_elf_gas.S",
            "src/asm/ontop_arm_aapcs_elf_gas.S",
        ],
        "@platforms//cpu:x86_32": [
            "src/asm/jump_i386_sysv_elf_gas.S",
            "src/asm/make_i386_sysv_elf_gas.S",
            "src/asm/ontop_i386_sysv_elf_gas.S",
        ],
        "@platforms//cpu:x86_64": [
            "src/asm/jump_x86_64_sysv_elf_gas.S",
            "src/asm/make_x86_64_sysv_elf_gas.S",
            "src/asm/ontop_x86_64_sysv_elf_gas.S",
        ],
    }),
)

filegroup(
    name = "windows_asm_msvc",
    srcs = select({
        "@platforms//cpu:x86_32": [
            "src/asm/jump_i386_ms_pe_masm.asm",
            "src/asm/make_i386_ms_pe_masm.asm",
            "src/asm/ontop_i386_ms_pe_masm.asm",
        ],
        "@platforms//cpu:x86_64": [
            "src/asm/jump_x86_64_ms_pe_masm.asm",
            "src/asm/make_x86_64_ms_pe_masm.asm",
            "src/asm/ontop_x86_64_ms_pe_masm.asm",
        ],
    }),
)

filegroup(
    name = "windows_asm_mingw",
    srcs = select({
        "@platforms//cpu:x86_32": [
            "src/asm/jump_i386_ms_pe_clang_gas.S",
            "src/asm/make_i386_ms_pe_clang_gas.S",
            "src/asm/ontop_i386_ms_pe_clang_gas.S",
        ],
        "@platforms//cpu:x86_64": [
            "src/asm/jump_x86_64_ms_pe_clang_gas.S",
            "src/asm/make_x86_64_ms_pe_clang_gas.S",
            "src/asm/ontop_x86_64_ms_pe_clang_gas.S",
        ],
    }),
)

filegroup(
    name = "windows_asm",
    srcs = select({
        ":win-mingw": [":windows_asm_mingw"],
        ":win-msvc": [":windows_asm_msvc"],
    }),
)

filegroup(
    name = "apple_asm",
    srcs = select({
        "@platforms//cpu:aarch64": [
            "src/asm/jump_arm64_aapcs_macho_gas.S",
            "src/asm/make_arm64_aapcs_macho_gas.S",
            "src/asm/ontop_arm64_aapcs_macho_gas.S",
        ],
        "@platforms//cpu:arm": [
            "src/asm/jump_arm_aapcs_macho_gas.S",
            "src/asm/make_arm_aapcs_macho_gas.S",
            "src/asm/ontop_arm_aapcs_macho_gas.S",
        ],
        "@platforms//cpu:x86_32": [
            "src/asm/jump_i386_sysv_macho_gas.S",
            "src/asm/make_i386_sysv_macho_gas.S",
            "src/asm/ontop_i386_sysv_macho_gas.S",
        ],
        "@platforms//cpu:x86_64": [
            "src/asm/jump_x86_64_sysv_macho_gas.S",
            "src/asm/make_x86_64_sysv_macho_gas.S",
            "src/asm/ontop_x86_64_sysv_macho_gas.S",
        ],
    }),
)

cc_library(
    name = "boost.context",
    srcs = select({
        "@platforms//os:macos": [
            "src/fcontext.cpp",
            "src/posix/stack_traits.cpp",
            ":apple_asm",
        ],
        "@platforms//os:windows": [
            "src/fcontext.cpp",
            "src/windows/stack_traits.cpp",
            ":windows_asm",
        ],
        "//conditions:default": [
            "src/fcontext.cpp",
            "src/posix/stack_traits.cpp",
            ":elf_asm",
        ],
    }),
    hdrs = glob(
        [
            "include/**/*.hpp",
        ],
        exclude = [
            "include/boost/context/continuation_winfib.hpp",
            "include/boost/context/fiber_winfib.hpp",
            "include/boost/context/posix/segmented_stack.hpp",
            "include/boost/context/preallocated.hpp",
            "include/boost/context/windows/protected_fixedsize_stack.hpp",
        ],
    ),
    defines = ["BOOST_ALL_NO_LIB"],
    features = ["parse_headers"],
    includes = ["include"],
    local_defines = select({
        "@platforms//os:windows": ["BOOST_CONTEXT_EXPORT="],
        "//conditions:default": [],
    }),
    textual_hdrs = [
        "include/boost/context/continuation_winfib.hpp",
        "include/boost/context/fiber_winfib.hpp",
        "include/boost/context/posix/segmented_stack.hpp",
        "include/boost/context/preallocated.hpp",
        "include/boost/context/windows/protected_fixedsize_stack.hpp",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "@boost.assert",
        "@boost.config",
        "@boost.core",
        "@boost.mp11",
        "@boost.pool",
        "@boost.predef",
        "@boost.smart_ptr",
    ],
)
